﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("YUIAspNet");

YUIAspNet.Dialog = function(element) {
    YUIAspNet.Dialog.initializeBase(this, [element]);
}

YUIAspNet.Dialog.prototype = {
    initialize: function() {
        YUIAspNet.Dialog.callBaseMethod(this, 'initialize');
    },

    initializeYUIControl: function(sender, args) {
       var dialogID = this._prepareDialog();
       this._yuiControl = new YAHOO.widget.Dialog(dialogID, this._getConfig());

	   this._updateContent(); 
    },

    _getCallbackFunction:  function(callback) 
    { 
        var me = this;
        return function () { callback(me, {})}; 
    },
    
    _prepareDialog: function() {
        var dlgID = this.get_element().id+"_dlg";
        var dlg = $get(dlgID);
        if(!dlg)
        {
            dlg = document.createElement("div");
            dlg.id = dlgID;
            $get(this._getRenderContainer()).appendChild(dlg);
        }
        return dlgID;
    },
    
    _updateContent: function() {
        if(this.get_HeaderContentID())
            this._yuiControl.setHeader($get(this.get_HeaderContentID()));
            
        if(this.get_FooterContentID())
            this._yuiControl.setFooter($get(this.get_FooterContentID()));
            
        this._yuiControl.setBody($get(this.get_BodyContentID()));
				 
	    this._yuiControl.render(this._getRenderContainer());
//	    
//	    new YAHOO.util.Resize(this.get_element().id+"_dlg", {
//                handles: ["br"],
//                autoRatio: false,
//                minWidth: 300,
//                minHeight: 100,
//                status: false 
//            });
    },
    
    _getContainerID: function() {
        return '__dialogContainer__'+ this.get_ThemeName();
    }, 
    
    _getRenderContainer: function() {
        var dialogContainer = $get(this._getContainerID());
        if(!dialogContainer)
        {
            dialogContainer = document.createElement("div");
            dialogContainer.id = this._getContainerID();
            dialogContainer.className = this.get_ThemeName();
            
            document.body.appendChild(dialogContainer);
        }
        return this._getContainerID();
    },
    
    _getConfig: function() {
        
        //process button handler...
        var buttonList = [];
        var me = this;
        for(var idx = 0 ; idx < this.get_DialogButtonList().length; idx++)
        {
            var actualHandler = this.get_DialogButtonList()[idx].handler;
            if(actualHandler)
            {
                var wrappedHandler = this._getCallbackFunction(actualHandler);
            }
            else wrappedHandler = function() { me.hide(); };
            
            buttonList.push(
                    { text: this.get_DialogButtonList()[idx].text, 
                        handler: wrappedHandler, 
                        isDefault: !!this.get_DialogButtonList()[idx].isDefault
                    });
        }
    
         var config =
             { 
               width: this.get_element().style.width || 'auto',
               height: this.get_element().style.height || 'auto',
		       fixedcenter: this.get_Centered(),
		       visible: !!this.get_Visible(),
		       draggable: !!this.get_Draggable(),
		       effect: this._getEffect(),
		       close: !!this.get_Closeable(),
		       icon: this.get_Icon(),
		       constraintoviewport: !!this.get_ConstrainToViewport(),
		       modal: !!this.get_Modal(),
		       buttons: buttonList,
		       underlay: this.get_Underlay()
		     };
		     
		     return config;
    },
    
    show: function() {
        if(this.get_ContentURL())
            this.setBody(String.format("<iframe frameborder='0' name='win' src='{0}' style='border: 0px none ; width: 100%; height: 100%; opacity: 0.99;'></iframe>", this.get_ContentURL()));
    
        this.get_YUIControl().show();
    },
    
    hide: function() {
        this.get_YUIControl().hide();
    },
    
    setHeader: function(headerContent) {
        this.get_YUIControl().setHeader(headerContent);
    },
    
    setBody: function(bodyContent) {
        this.get_YUIControl().setBody(bodyContent);
    },
    
    setFooter: function(footerContent) {
        this.get_YUIControl().setFooter(footerContent);
    },
    
    get_ContentURL: function() {
        return this._contentURL;
    },
    set_ContentURL: function(contentURL) {
        this._contentURL = contentURL;
    },

    get_Centered: function() {
        return this._centered;
    },
    set_Centered: function(centered) {
        this._centered = centered;
    },
    
    get_ContainerID: function() {
        return this._containerID;
    },
    set_ContainerID: function(containerID) {
        this._containerID = containerID;
    },
    
    get_ThemeName: function() {
        return this._themeName;
    },
    set_ThemeName: function(themeName) {
        this._themeName = themeName;
    },
    
     get_Visible: function() {
        return this._visible;
    },
    set_Visible: function(visible) {
        this._visible = visible;
    },
    
    get_Draggable: function() {
        return this._draggable;
    },
    set_Draggable: function(draggable) {
        this._draggable = draggable;
    },
      
    get_Closeable: function() {
        return this._closeable;
    },
    set_Closeable: function(closeable) {
        this._closeable = closeable;
    },
    
    get_ConstrainToViewport: function() {
        return this._constrainToViewport;
    },
    set_ConstrainToViewport: function(constrainToViewport) {
        this._constrainToViewport = constrainToViewport;
    },
    
    get_BodyContentID: function() {
        return this._bodyContentID;
    },
    set_BodyContentID: function(bodyContentID) {
        this._bodyContentID = bodyContentID;
    },
    
    get_HeaderContentID: function() {
        return this._headerContentID;
    },
    set_HeaderContentID: function(headerContentID) {
        this._headerContentID = headerContentID;
    },
    
    get_FooterContentID: function() {
        return this._footerContentID;
    },
    set_FooterContentID: function(footerContentID) {
        this._footerContentID = footerContentID;
    },
  
    get_Icon: function() {
        return this._icon;
    },
    set_Icon: function(icon) {
        this._icon = icon;
    },
  
    get_Modal: function() {
        return this._modal;
    },
    set_Modal: function(modal) {
        this._modal = modal;
    },
  
    get_DialogButtonList: function() {
        return this._dialogButtonList;
    },
    set_DialogButtonList: function(dialogButtonList) {
        this._dialogButtonList = dialogButtonList;
    },
    
    get_Underlay: function() {
        return this._underlay;
    },
    set_Underlay: function(underlay) {
        this._underlay = underlay;
    }
}

YUIAspNet.Dialog.registerClass('YUIAspNet.Dialog', YUIAspNet.BaseControl);
